#!perl

#*******************************************************}
#                                                       }
#       Yalgol/02 Compiler, Version 0.02 beta           }
#       Written by Y [29-06-03] <yallie@mail.ru>        }
#                                                       }
#       Smart assembling utility                        }
#                                                       }
#       Copyright (c) 1999-2003 YALLIE, Inc.            }
#       All Rights Reserved.                            }
#                                                       }
#*******************************************************}

# $Id: smartasm.pl,v 1.2 2003/07/01 02:20:50 Y Exp $

# Assembles the file for smart linking technology
# Produces .lib instead of .obj
# Arguments: file, temp path, asm command, lib command

# get arguments either from the command line or
# responce file

$FileName = $ARGV[0];
if (substr($FileName, 0, 1) eq "@") { 
  # responce file
  open ARGS, substr($FileName, 1, length($FileName) - 1);
  $FileName = <ARGS>;
  $LibName = <ARGS>;
  $TempPath = <ARGS>;
  $AsmCommandTemplate = <ARGS>;
  $LibCommandTemplate = <ARGS>;
  close ARGS;
} else {
  # command line
  $LibName = $ARGV[1];
  $TempPath = $ARGV[2];
  $AsmCommandTemplate = $ARGV[3];
  $LibCommandTemplate = $ARGV[4];
}

die("Smart assembling utility, version 0.00 alpha
Written by Y [29-06-03]. Use y02c -s to produce assemblies.

Syntax: $0 \"file.asm\" \"file.lib\" \"temp path\" \"asm template\" \"lib template\"

Too few arguments") 
  if !defined($FileName) || !defined($TempPath) || 
    !defined($AsmCommandTemplate) || !defined($LibCommandTemplate);

# strip extra spaces/newlines
$FileName =~ s/^\s*(.*?)\s*$/$1/;
$LibName =~ s/^\s*(.*?)\s*$/$1/;
$TempPath =~ s/^\s*(.*?)\s*$/$1/;
$AsmCommandTemplate =~ s/^\s*(.*?)\s*$/$1/;
$LibCommandTemplate =~ s/^\s*(.*?)\s*$/$1/;

# debuggin'
#print "$FileName\n$LibName\n$TempPath\n$AsmCommandTemplate\n$LibCommandTemplate\n";

# validate input assembly and count symbols
open INFILE, "$FileName" || die("Can't open $FileName");
while (<INFILE>) {
  $Stop = (/SYMBOL_COUNT\s*equ\s*(\d+)/);
  $TotalSymbols = $1 if $1;
  last if $Stop;
}
close INFILE;

# assemble all symbols and create object list
$ListFileName = $LogFileName = $LibName;
$ListFileName =~ s/\.lib$/\.lst/i;
$ListFileName =~ s/.*?([^\/\\]*)$/$1/i;
$LogFileName = ~ s/\.lib$/\.log/i;
$LogFileName = ~ s/.*?([^\/\\]*)$/$1/i;
open OUTFILE, ">$TempPath\\$ListFileName";

die("$FileName doesn't support smart linking") if !$TotalSymbols;

for ($i = 1; $i <= $TotalSymbols; $i++) {
  # assemble
  $AsmFileName = "$TempPath\\$i.obj";
  $Command = $AsmCommandTemplate . "$i $FileName, $AsmFileName\n";
  print `$Command`;
  print OUTFILE "+$AsmFileName ";
}

close OUTFILE;

# create library from the list
unlink "$LibName";
$Command = $LibCommandTemplate . " $LibName \@$TempPath\\$ListFileName";
`$Command`;

# delete intermediate files
for ($i = 1; $i <= $TotalSymbols; $i++) {
  unlink "$TempPath\\$i.obj";
}
unlink "$TempPath\\$ListFileName";

# $Log: smartasm.pl,v $
# Revision 1.2  2003/07/01 02:20:50  Y
#   - code that renames .asm file before assembling
#
# Revision 1.1  2003/06/29 15:51:37  Y
#   * this utility handles .asm -> .lib compiling (y02c build 149)
#   + initial checkin
#
